<?php

namespace App\Modules\Inventory\Controllers;

use App\Lib\Utilities;
use App\Modules\Inventory\Services\GoodsService;
use App\Modules\Inventory\Validators\GoodsControllerValidator;
use App\Modules\System\Services\ConfigService;
use Flower\Core\Http\BaseController;
use Flower\Core\Repository\Exceptions\ValidatorException;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Routing\Controller;

class GoodsController extends BaseController
{
    protected $goodsService;
    protected $configService;

    public function __construct(GoodsService $goodsService, ConfigService $configService)
    {
        $this->goodsService = $goodsService;
        $this->configService = $configService;
    }

    public function getRules()
    {
        return GoodsControllerValidator::validator();
    }

    /*
     * 新增商品
     */
    public function createGoods(Request $request)
    {
        $store_id = $request->input('store_id');
        $cat_id = $request->input('cat_id');
        $name = $request->input('name');
        $small_name = $request->input('small_name');
        $buying_price = $request->input('buying_price');
        $market_price = $request->input('market_price');
        $royalty = $request->input('royalty');
        $volume = $request->input('volume');
        $specs = $request->input('specs');
        $is_sell = $request->input('is_sell');
        $image = $request->input('image');

        $goods = $this->goodsService->createGoods(
            $store_id, $cat_id, $name, $small_name, $buying_price, $market_price, $royalty,
            $volume, $specs, $is_sell, $image
        );

        $data = [
            'goods_id' => $goods->id
        ];

        return $this->display($data, '添加成功');

    }

    /**
     * 编辑商品
     * @param Request $request
     * @return mixed
     */
    public function editGoods(Request $request)
    {
        $goods_id = $request->input('goods_id');
        $cat_id = $request->input('cat_id');
        $name = $request->input('name');
        $small_name = $request->input('small_name');
        $buying_price = $request->input('buying_price');
        $market_price = $request->input('market_price');
        $royalty = $request->input('royalty');
        $volume = $request->input('volume');
        $specs = $request->input('specs');
        $is_sell = $request->input('is_sell');
        $image = $request->input('image');
        $this->goodsService->editGoods($goods_id, $cat_id, $name, $small_name, $buying_price, $market_price, $royalty, $volume, $specs, $is_sell, $image);

        return $this->success('操作成功');

    }

    /**
     * 获取商品规格
     * @param Request $request
     * @return mixed
     * @throws ValidatorException
     */
    public function getGoodsSpecs()
    {
        $data = $this->configService->getDictionary('goods_specs');
        return $this->display($data, '查询成功');
    }

    /**
     * 删除商品
     * @param Request $request
     * @return mixed
     */
    public function delGoods(Request $request)
    {
        $goods_id = $request->input('goods_id');
        $this->goodsService->delGoods($goods_id);

        return $this->success('删除成功');

    }

    /**
     * 获取本店所有外卖商品
     * @param Request $request
     * @return mixed
     * @throws \App\Exceptions\AllValidatorException
     */
    public function getSellGoodsList(Request $request)
    {

        $store_id = $request->input('store_id');
        $keywords = $request->input('keywords');
        $pageSize = $request->input('pageSize', 10);
        $data = $this->goodsService->getSellGoodsList($store_id, $keywords, $pageSize);

        return $this->display($data, '返回成功');
    }

    /**
     * 获取本店商品列表 搜索
     * @param Request $request
     * @return mixed
     */

    public function getGoodsListSearch(Request $request)
    {
        $store_id = $request->input('store_id');
        $keywords = $request->input('keywords');
        $f_class = $request->input('f_class');
        $s_class = $request->input('s_class');


        $goods_list = $this->goodsService->getGoodsListSearch($store_id, $keywords, $f_class, $s_class);

        return $this->display($goods_list, '返回成功');

    }

    /**
     * 根据id获取商品
     * @param Request $request
     * @return mixed
     */
    public function getGoods(Request $request)
    {
        $goods_id = $request->input('goods_id');
        $goods = $this->goodsService->getGoods($goods_id);
        return $this->display($goods, '返回成功');
    }

}
